package com.yeyks.service.impl;

import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.common.appcommon.dto.AppAddressCode;
import com.yeyks.common.appcommon.result.CommonResult;
import com.yeyks.common.em.pay.PayTypeEnum;
import com.yeyks.common.utils.Utility;
import com.yeyks.commonReference.append.wxPay.dto.WxRefundParam;
import com.yeyks.entity.AppAccountInfo;
import com.yeyks.order.dal.domain.OrderHead;
import com.yeyks.other.service.AddressDivisionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * 共同参数的 公用类
 *
 * @author Administrator
 */
public abstract class AbstractAppCommon {

    @Autowired
    private AddressDivisionService addressService;


    /**
     * 微信退款参数信息
     *
     * @param orderHead 订单相关细腻
     * @return 返回参数
     */
    protected WxRefundParam setWechatRefundParam(OrderHead orderHead) {

        int totalFee = 0;
        int refundFee = 0;
        if (null != orderHead) {
            totalFee = orderHead.getSnapshotTotalFee();
            refundFee = orderHead.getSnapshotTotalFee();
        }
        if (refundFee == 0 || refundFee > totalFee) {
            refundFee = totalFee;
        }

        assert orderHead != null;
        String payType = PayTypeEnum.getDesc(orderHead.getPaymentMethod());
        return new WxRefundParam(payType,
                orderHead.getOrderNo(),
                (System.currentTimeMillis() + Utility.randomString(7)),
                totalFee,
                refundFee);
    }


    /**
     * 补全添加 代理的参数信息
     *
     * @param addAccountInfo 添加的参数
     * @param accountInfo    操作人的对象
     */
    protected void convertAccountInfo(AccountInfo addAccountInfo, AppAccountInfo accountInfo, String divCode) {

        //设置parentId
        addAccountInfo.setParentId(accountInfo.getId());
        //设置area 小数点隔开
        addAccountInfo.setArea(this.getAddressArea(divCode));
        //设置 省市区的code值
        AppAddressCode appAddressCode = this.getAddressCode(divCode);
        if (null != appAddressCode) {
            addAccountInfo.setProvinceCode(appAddressCode.getProvinceCode());
            if (!StringUtils.isEmpty(appAddressCode.getCityCode())) {
                addAccountInfo.setCityCode(appAddressCode.getCityCode());
                if (StringUtils.isEmpty(appAddressCode.getCountyCode())) {
                    addAccountInfo.setCountryCode(appAddressCode.getCountyCode());
                }
            }
        }
        //设置默认启用状态
        addAccountInfo.setValid(true);
        //设置账户类型，这个根据规律，建立的子账号相当于 （上级 + 1） 如果后期规律有变，则另外考虑
        addAccountInfo.setType(accountInfo.getType() + 1);
        addAccountInfo.setGmtCreate(new Date());
        addAccountInfo.setGmtModified(new Date());

    }

    /**
     * 拼接area
     *
     * @param divCode 地址的code
     * @return
     */
    private String getAddressArea(String divCode) {

        CommonResult<String> addressDTOCommonResult = addressService.getAreaByDivId(divCode);
        if (addressDTOCommonResult.getCode() == CommonResult.SUCCESS) {
            return addressDTOCommonResult.getData();
        }
        return null;
    }

    /**
     * 得到省，市，区的 Code 对象
     *
     * @param divCode 传输的code
     * @return 返回 对象
     */
    private AppAddressCode getAddressCode(String divCode) {

        CommonResult<AppAddressCode> appAddressCodeCommonResult = addressService.getAddressCodeByDivId(divCode);
        if (appAddressCodeCommonResult.getCode() == CommonResult.SUCCESS) {
            return appAddressCodeCommonResult.getData();
        }
        return null;
    }

}
